package com.telecom.config;

import com.telecom.aspect.interceptor.TokenInterceptor;
import com.telecom.common.json.JacksonObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Web MVC配置类
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Resource
    private TokenInterceptor tokenInterceptor;

    @Resource
    private JacksonObjectMapper jacksonObjectMapper;

    /**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册token拦截器
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns(
                        "/admin/employee/login",  // 管理员登录
                        "/user/person/login",     // 用户登录
                        "/error",                 // 错误页面
                        "/favicon.ico"            // 图标请求
                        // 根据需要添加更多不需要验证的路径
                );
    }

    /**
     * 扩展消息转换器
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建消息转换器
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        // 设置对象转换器，使用我们自定义的JacksonObjectMapper
        messageConverter.setObjectMapper(jacksonObjectMapper);
        // 将我们的转换器放到最前面，优先使用我们的转换器
        converters.add(0, messageConverter);
    }
} 